package com.hbt.yiqing.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.yiqing.R;
import com.hbt.yiqing.db.UserTable;
import com.hbt.yiqing.entity.Users;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllowActivity extends AppCompatActivity {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    private Cursor c;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow);
        ButterKnife.bind(this);
        initRecyler();
    }

    void initRecyler(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        adapter = new MyAdapter(getUsersByAllow());
        recycle.setAdapter(adapter);
    }

    List<Users> getUsersByAllow(){
        List<Users> users = new ArrayList<>();
        UserTable userTable = new UserTable(this);
        SQLiteDatabase db = userTable.getWritableDatabase();
        ContentValues values = new ContentValues();
        c = db.query(UserTable.TABLE_NAME,null,UserTable.ALLOW+"=?",new String[]{"0"},null,null,null);
        for (int i = 0; i < c.getCount(); i++){
            c.moveToPosition(i);
            Users user = new Users();
            user.setUsername(c.getString(c.getColumnIndex(UserTable.USERNAME)));
            user.setInfo(c.getString(c.getColumnIndex(UserTable.INFO)));
            users.add(user);
        }
        return users;
    }

    protected class MyAdapter extends RecyclerView.Adapter{
        protected List<Users> users;
        public MyAdapter (List<Users> users){
            this.users = users;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout layout = new LinearLayout(parent.getContext());
            layout.setLayoutParams(params);
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.apply_item,layout,true);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder hold = (ViewHolder) holder;
            hold.tv_username.setText(users.get(position).getUsername());
            hold.tv_info.setText(users.get(position).getInfo());
            hold.btn_allow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UpdateAllow(hold);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(AllowActivity.this, "授权成功", Toast.LENGTH_LONG).show();
                }
            });
        }//onBindViewHolder end
        //设置为授权
        void UpdateAllow(ViewHolder hold){
            UserTable userTable = new UserTable(getBaseContext());
            SQLiteDatabase db = userTable.getWritableDatabase();
//            c = db.query(UserTable.TABLE_NAME,null,UserTable.USERNAME+"=?",new String[]{hold.tv_username.getText().toString()},null,null,null);
            //修改对应User
            ContentValues values = new ContentValues();
            values.put(UserTable.USERNAME, hold.tv_username.getText().toString());
            values.put(UserTable.ALLOW, "1");
            db.update(UserTable.TABLE_NAME, values,UserTable.USERNAME+"=?",new String[]{hold.tv_username.getText().toString()});
            users.remove(getUserByUsername(hold.tv_username.getText().toString()));
        }

        Users getUserByUsername(String username){
            for(Users u:users){
                if (u.getUsername().equals(username))return u;
            }
            return null;
        }

        @Override
        public int getItemCount() {
            return users.size();
        }
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView tv_username;
            TextView tv_info;
            Button btn_allow;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_username = itemView.findViewById(R.id.username);
                tv_info= itemView.findViewById(R.id.info);
                btn_allow = itemView.findViewById(R.id.btn);
            }
        }

    }// not main class end



}//main class end
